<?php

defined('_JEXEC') or die();

jimport('joomla.application.component.controller');
require_once(JPATH_COMPONENT.DS.'helpers'.DS.'ftphelper.php');
require_once(JPATH_COMPONENT.DS.'helpers'.DS.'remotehelper.php');

class Easiv2ControllerEditInstances extends JController {

	var $remoteHelper;

	function __construct() {
		parent::__construct();

		$this->remoteHelper = new RemoteHelper();
	}

	function cancel() {
		JRequest::setVar('view', 'manageInstances');

		parent::display();
	}

	/**
	 * Function to update instances in the database
	 */
	function update() {
		$ids = JRequest::getVar('id');
		$db = JFactory::getDBO();

		foreach($ids as $id) {
			$newName = JRequest::getVar($id.'_name');
			$newHttp = JRequest::getVar($id.'_http');
			$newFtp = JRequest::getVar($id.'_ftp');
			$newPort = JRequest::getVar($id.'_port');
			$newFolder = JRequest::getVar($id.'_folder');
			$newUser = JRequest::getVar($id.'_user');
			$newPass = JRequest::getVar($id.'_pass');
			$current_identifier = JRequest::getVar($id.'_current_identifier');
			$new_identifier = JRequest::getVar($id.'_new_identifier');
			$new_identifier2 = JRequest::getVar($id.'_new_identifier2');			
			
			/*
			 * check and modify userinput for correct syntax if nescessary
			 */
			if($newName == "") {
				$newName = 'Default Site';
			}

			if($newPort == "") {
				$newPort = 21;
			}
			
			if(substr($newHttp, strlen($newHttp)-1) != '/') {
				$newHttp .= '/';
			}
			
			if(substr($newHttp, 0, 7) != "http://") {
				$newHttp = 'http://'.$newHttp;
			}
	
			/*
			 * check for correct input of ftp host. Joomla! requires no 'ftp://' at the beginning and no '/' at the end
			 * Also 'localhost' does not work...
			 */
			//check delete a '/' at the end
			if(substr($newFtp, strlen($newFtp)-1) == '/') {
				$newFtp = substr($newFtp, 0, strlen($newFtp)-1);
			}
			
			//remove 'ftp://'
			if(substr($newFtp, 0, 6) == 'ftp://') {
				$newFtp = substr($newFtp, 6);
			}
			
			//turn 'localhost' into 127.0.0.1
			if($newFtp == "localhost") {
				$newFtp = "127.0.0.1";
			}

			echo "<h3>Instance ID: ".$id."</h3>";

			if($newUser != "" && $newPass != "" && $newFtp != "" && $newHttp != "") {
				/*
				 * Check, if Joomla! instance is responding
				 */
				$response = $this->remoteHelper->isInstanceResponding($newHttp);
				$setIdentifier = false;

				$db->setQuery("SELECT identifier FROM #__easiv2_instances WHERE id = ".$id);
				$currentDBIdentifier = $db->loadObject()->identifier;

				if($response == "true") {
					/*
					 * If new_identifier is not empty, the user wants to set it. So we check, if there is a current identifier set in the databse.
					 * According to this, we change it or set it.
					 */
					if($new_identifier != "") {
						if($currentDBIdentifier == "") {
							if($new_identifier == $new_identifier2) {
								/*
								 * Trying to set identifier in remote instance
								 */
								$response = $this->remoteHelper->setIdentifier($newHttp, $current_identifier, $new_identifier);
								
								if($response == "identifierWrong") {
									echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_CURRENTIDENTIFIERWRONG')."</b></span><br>";
								}
								else {
									$setIdentifier = true;
								}
							}
							else {
								echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_IDENTIFIERSDONTMATCH')."</b></span><br>";
							}
						}
						else {
							if($new_identifier == $new_identifier2) {
								/*
								 * Trying to set identifier in remote instance
								 */
								$response = $this->remoteHelper->setIdentifier($newHttp, $current_identifier, $new_identifier);

								if($response == "identifierWrong") {
									echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_CURRENTIDENTIFIERWRONG')."</b></span><br>";
								}
								else {
									$setIdentifier = true;
								}
							}
							else {
								echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_IDENTIFIERSDONTMATCH')."</b></span><br>";
							}
						}
					}
					else {
						if($current_identifier == $currentDBIdentifier) {
							if($new_identifier == $new_identifier2) {
								/*
								 * Trying to set identifier in remote instance
								 */
								$response = $this->remoteHelper->setIdentifier($newHttp, $current_identifier, $new_identifier);
								
								if($response == "identifierWrong") {
									echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_CURRENTIDENTIFIERWRONG')."</b></span><br>";
								}
								else {
									$setIdentifier = true;
								}
							}
							else {
								echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_IDENTIFIERSDONTMATCH')."</b></span><br>";
							}
						}
						else {

						}
					}
					
					/*
					 * Check FTP and update database
					 */
					$ftp = new FtpHelper();
					if($ftp->isFtpValid($newFtp, $newPort, $newUser, $newPass)) {
						$db->setQuery("UPDATE #__easiv2_instances SET name = '".$newName."', http_address = '".$newHttp."', ftp_host = '".$newFtp."', port = '".$newPort."', host_folder = '".$newFolder."', ftp_user = '".$newUser."', ftp_pass = '".$newPass."' WHERE id = ".$id);

						if($db->query() != false) {
							if($setIdentifier) {
								$db->setQuery("UPDATE #__easiv2_instances SET identifier = '".$new_identifier."' WHERE id = ".$id);
								$db->query();

								echo "<img src=\"components/com_easiv2/images/true.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_IDENTIFIERSET')."<br>";
							}
							else {
								echo JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_IDENTIFIERNOCHANGES')."<br>";
							}

							echo "<img src=\"components/com_easiv2/images/true.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_UPDATESUCCESSFUL')."<br>";
						}
						else {
							echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_UPDATEFAILED')."<br>";
						}
					}
					else {
						echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_FTPCONNECTIONFAILED')."<br>";
					}
				}
				else {
					echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_EDITINSTANCES_CONTROLLER_INSTANCECONNECTIONFAILED')."<br>";
				}
			}
			else {
				echo "<img src=\"components/com_easiv2/images/false.png\" /> ".JText::_('COM_EASIV2_MANAGEINSTANCES_CONTROLLER_INFORMATIONMISSING')."<br>";
			}
		}
			
		echo "<hr />";

		JRequest::setVar('view', 'editInstances');
		parent::display();
	}
}